Eureka to achieve data transfer service between the use of SpringCloud

    I believe the theory is certainly not broken a lot of what we are most concerned about, and also the half-comprehended, was most concerned about than the parameters passed between services, data acquisition.

    Ok, today will tell you the way to transfer data between the three micro-services are:

1, using the basic Ip port interface requests access to the data transmission

2, the use of Eureka substituted Ip (hard-coded) data transmission means to achieve

3, using Feign more efficient, "Excellent service" approach to achieve data transfer between micro-services (for Feign do not understand that being without understanding that SpringCloud consisting of a part of the latter will be explained in detail, explain here just for yourself mark it)

    Premise : to build a good registry Eureka, suppose we have a micro-order service (service consumers), a micro-user service (service provider), then the user needs to order micro-micro-service call service, user access to information, and two service It can function properly, in order to facilitate the use of SpringJpa the way here to get the data.

First, the use RestTemplate + Ip ways:

1, most of the data transmission is passed as a set of objects and, so here the manner of Example collections and objects

Written query method is called in user micro-services (service provider) in

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserRepository userRepository;

    @GetMapping("/getallUser")
    public List<User> getUserPage(){
        return userRepository.findAll();
    }

    @GetMapping("/getUser")
    public User getUser(Long userId){
        return userRepository.getOne(userId);
    }

}
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
@Entity
@Table(name="TM_USER")
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"})
public class User implements Serializable, Cloneable {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long userId;

    /**
     * 登录名
     */
    @Column(name = "USER_NAME")
    private String userName;

    /**
     * 用户名
     */
    @Column
    private String name;

    /**
     * 薪水
     */
    @Column
    private BigDecimal balance;

    /**
     * 年龄  get set方法已省略 自行添加
     */
    @Column
    private int age;}

Ok, service providers have written, you can start writing service consumers

2, adding the Bean in order to generate RestTemplate micro-services (service consumers) start classes in 

@SpringBootApplication
@EnableEurekaClient		//启用eureka客户端
public class MicroserviceSimplecConsumerOrderServerApplication {

	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}

	public static void main(String[] args) {
		SpringApplication.run(MicroserviceSimplecConsumerOrderServerApplication.class, args);
	}
}

In order micro-services (service consumers) profile yml added, which localhost: 9021 read user micro-services (Ip and port service providers)

user:
  urlPath: http://localhost:9021

Written order service control layer, User here to copy the user's micro-services and then remove the @ Column, @ Table annotation, such as only a temporary storage database objects and no association

@RestController
@RequestMapping("/order")
public class OrderServerController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${user.urlPath}")
    private String urlPath;
    //获取集合方式
    @GetMapping("/allUser")
    private List<User> getAllUser(){
        ResponseEntity<List<User>> responseEntity = restTemplate.exchange("urlPath"+/user/getallUser?",
                HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>() {});
        return responseEntity.getBody();
    }
    //获取单个对象方式
    @GetMapping("/getUser")
    private User getUser(Long userId){
        User user = restTemplate.getForObject("urlPath"+/user/getUser?userId="+userId,User.class);
        return user;
    }
}

Second, use way to obtain Eureka

Was simply orders directly to the micro-services (service consumers) OrderServerController in userPath replaced Eureka registered service name to the above is that simple. Assume that the user of the service, called micro-services: microservice-privider-user

spring:
  application:
    name: microservice-privider-user

Then the replacement process can be carried out directly, i.e.:

@GetMapping("/allUser")  //返回集合
   private List<User> getAllUser(){
        ResponseEntity<List<User>> responseEntity = restTemplate.exchange("microservice-privider-user"+"/user/getallUser", HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>() {});
        return responseEntity.getBody();
    }
//返回对象
    @GetMapping("/getUser")
    private User getUser(Long userId){
        User user = restTemplate.getForObject("microservice-privider-user"+/user/getUser?userId="+userId,User.class);
        return user;
    }

Ok, use the second approach has been prepared

Using the service name to replace the fixed ip, Ip hard-coded addresses the difficult problem of maintenance

As for the third Feign way would later explain: explain in detail "Feign declarative Rest client" when

gitHub Address: https://github.com/mackjie/microservice-spring-cloud because the configuration of the RabbitMQ, so please read the ReadMe and then start the project, otherwise it will start error

Published 21 original articles · won praise 8 · Views 400,000 +

Guess you like

Origin blog.csdn.net/qq_31150503/article/details/79919563