发表于: 2018-09-10 21:15:07

1 529


今天完成的事情:

消费者设置提供者的负载均衡策略

默认已经开启了负载均衡(feign的负载均衡设置方式相同)
1.添加一个新的提供者:(同服务名)
新提供者yml配置文件:
server:
port: 8081
eureka:
client:
service-url:
spring:
application:
name: provide-service
原提供者yml配置文件:
server:
port: 8080
eureka:
client:
service-url:
spring:
application:
name: provide-service
2.消费者通过yml配置选择负载均衡策略
#配置提供者负载均衡策略
provide-service:
ribbon:

NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule


消费者参数传递篇(ribbon)

GET方法:
返回基本类型:
1.提供者添加接口:
@RequestMapping(value = "/parameter/{name}")
public String parameter(@PathVariable("name") String name){
return "parameter: "+name;
}
2.消费者访问接口:
@RequestMapping(value = "/parameter/{name}")
public String parameter(@PathVariable("name") String name){
System.out.println(name);
return restTemplate.getForEntity("http://hello-service/parameter/name={1}",String.class,name).getBody();
}
3.调用提供者接口:http://127.0.0.1:8081/parameter/z
4.调用消费者接口:http://127.0.0.1:9000/parameter/z
返回对象:
1.提供者、消费者创建实体类文件夹
2.实体类(无参构造函数<必须>):
public class Student {
private String name;
private Long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Student(String name, Long id) {
this.name = name;
this.id = id;
}
public Student() {
}
@Override
public String toString() {
return "Student{" + "name='" + name + '\'' + ", id=" + id + '}';
}
}
2.提供者编写接口:
@RequestMapping(value = "/object",method = RequestMethod.GET)
public Student myObject(@RequestParam("name")String name,@RequestParam("id")long id){
return new Student(name,id);
}
3.消费者接口:
@GetMapping(value = "/object")
public Student myObject(){
return restTemplate.getForEntity("http://hello-service/object?name={1}&id={2}",Student.class,"小明",1L).getBody();
}
4.postman测试提供者接口:
5.postman测试消费者接口:
getForEntity与getForObject
提供者:
@GetMapping(value = "/getTest")
public Student getTest(@RequestParam("name")String name,@RequestParam("id")long id){
return new Student(name,id);
}
消费者:
@GetMapping(value = "/getTest")
public Student getTest(){
return restTemplate.getForObject("http://hello-service/getTest?name={1}&id={2}",Student.class,"小米",2L);
}
POST方法:
提供者:
@PostMapping(value = "/postTest")
public Student postTest(@RequestBody Student student){
return student;
}
消费者:
@PostMapping(value = "/postTest")
public Student postTest(){
Student student=new Student("小王",3L);
return restTemplate.postForObject("http://hello-service/postTest",student,Student.class);
}
postman测试提供者:
postman测试消费者:
put请求:(无返回值)
提供者:
@PutMapping(value = "/putTest")
public void putTest(@RequestBody Student student){
System.out.println(student);
}
消费者:
@PutMapping(value = "putTest")
public void putTest(){
Student student=new Student("南航",5L);
restTemplate.put("http://hello-service/putTest",student);
}
postman测试:
提供者输出:
Delete
提供者:
@RequestMapping(value = "/delete")
public void delete(@RequestParam("id") long id){
System.out.println(id);
}
消费者:
@RequestMapping("/delete")
public void delete() {
restTemplate.delete("http://hello-service/delete?id={1}", 100L);
}
postman测试:
提供者输出:
@RequestParam与@PathVariable
@RequestParam对应"http://hello-service/delete?id={1}", 100L

@PathVariable对应"http://hello-service/delete2/{1}", 100L


消费者(Feign篇)

1.添加依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 系统注册与监测服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 系统配置管理中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- spring 配置读取(@ConfigurationProperties(prefix = "test")) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- feign 声明式服务调用框架 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- feign httpclient,提高http性能 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
2.启动类添加注解
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class DemoFeignApplication {
public static void main(String[] args) {
SpringApplication.run(DemoFeignApplication.class, args);
}
}
3.yml文件
spring:
application:
name: eureka-feign
server:
port: 9001
eureka:
client:
service-url:
feign:
httpclient:
enabled: true
4.创建service(根据提供者的controller创建)
package com.jnshu.demofeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("provide-service")
public interface TestService {
@GetMapping(value = "/test/{name}")
String test(@PathVariable("name") String name);
}
5.创建controller
package com.jnshu.demofeign.controller;
import com.jnshu.demofeign.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class testController {
@Autowired(required = false)
private TestService testService;
@RequestMapping(value = "/test")
public String test(){
return testService.test("feign");
}
}
POST请求:
1.在提供者的控制层中添加路由:
@PostMapping(value = "/post")
public Student post(@RequestBody Student student){
student.setId(student.getId()+1);
return student;
}
2.在feign中添加service:
@PostMapping(value = "/post")
Student post(@RequestBody Student student);
3.在feign中添加controller中添加路由调用service
@PostMapping(value = "/post")
private Student post(){
return testService.post(new Student("测试学生",1));
}
4.通过postman测试接口:



明天计划的事情:

了解Oauth2

遇到的问题:


收获:


如上


返回列表 返回列表
评论

    分享到