SpringCloud Getting Started 2

Connected to a

The establishment of a producer
sub-module eureka-provider

1. In your own pom.xml to add a parent-child relationship references 

2. Add submodule eureka-provider in the main module pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zking</groupId>
        <artifactId>springcloud001</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.zking</groupId>
    <artifactId>eureka-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-provider</name>
    <packaging>jar</packaging>
    <description>生产者</description>

</project>

Development steps:
1. Modify the sub-module application.yml eureka-provider's
see information on "Eureka-provider.yml"
2. Create a ProviderController, serving
the Controller
Service
Mapper
Model
db
3. Add @EnableEurekaClient notes on startup class EurekaProviderApplication.java, click to run,
be careful not to turn off the start-Server before Eureka
4. Check the registry, you can see the producers of "eureka-provider"
to open the browser, type: http: // localhost: 7101

 

 

 Open the browser would not display pull, because I turned off

 

Then create a consumer 01
to create a sub-module eureka-consumer01
1. In reference to their parent-child relationship pom.xml add
2. Add a sub-module in the main module pom.xml eureka-consumer01

开发步骤
1.修改子模块eureka-consumer的application.yml
见资料“eureka-consumer01.yml”
2.创建Consumer01Controller,消费服务
注意:本案例使用RestTemplate方式来调用服务端的接口,
##1.RestTemplate配置代码:
@Configuration
public class RestConfig {
@Bean
@LoadBalanced //Ribbon负载均衡(手动开启)
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

##2.消费者消费生产者提供的服务关键代码
##Consumer01Controller注入并调用restTemplate
String url="http://localhost:7201/provider/hello";
String message = restTemplate.getForObject(url, String.class);

3.在启动类EurekaConsumer01Application.java上添加@EnableEurekaClient注解,点击运行即可,
注意不要关闭之前启动的eureka-server/eureka-provider
4.查看注册中心,即可看见消费者者“eureka-consumer01”
打开浏览器,输入:http://localhost:7101
5.打开浏览器,输入:http://localhost:7301/consumer01/hello
此步骤即为微服务外部的人或系统对微服务系统(springcloud01)的内部进行访问

=================================================================================

PRC远程调用的两种方式:Rest方式/Fegin方式
1.Rest方式
消费者01就使用的是这种方式,另外还可以参考“附录六:RestTemplate”了解这种方式的优缺点
2.Fegin方式
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。
它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。
Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

 

创建一个消费者02
创建子模块eureka-consumer02
1.在自己的pom.xml添加父子引用关系
2.在主模块的pom.xml添加子模块eureka-consumer02
3.使用“https://start.spring.io/”生成工程所需pom文件
搜索条件为:openfeign而非Fegin

 

 

 

创建Consymer02Controller

 

 

 

 在启动类EurekaConsumer02Application,java上添加@EnableEurekaClient和@EnableFeignClients注解,点击运行即可重点!!!!!!!!重点ps电脑忽然间有点毛病

 

 

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!上面是重点

 

 以上就完成了明天是公共模块和远程调用

 

 

Guess you like

Origin www.cnblogs.com/xiatian3452/p/11968568.html
Recommended