spring cloud 初体验
程序员文章站
2022-11-13 20:39:23
spring cloud分为注册端、客户端以及消费端 初体验的理解就是: 注册端就是将之前所有的应用在这边进行注册,然后给每个应用都生成自己的标识,这些应用就是来自于客户端,消费端则通过调用注册端(有点数据中心的概念)然后去调用客户端各个的应用. 1、注册端代码: 直接创建maven项目: pom. ......
spring cloud分为注册端、客户端以及消费端
初体验的理解就是:
注册端就是将之前所有的应用在这边进行注册,然后给每个应用都生成自己的标识,这些应用就是来自于客户端,消费端则通过调用注册端(有点数据中心的概念)然后去调用客户端各个的应用.
1、注册端代码:
直接创建maven项目:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.tp.soft</groupid> <artifactid>cloud-service</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>eureka-service</name> <description>forward project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <spring-cloud.version>finchley.sr1</spring-cloud.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
application.yml
#本项目端口 server: port: 8761 #服务注册中心实例主机名 eureka: instance: hostname: host client: #是否向注册中心注册自己 register-with-eureka: false #是否获取注册表 fetch-registry: false #服务地址,向哪里注册的地址,如果没有下面的注册地址但是开启了上面的 #想注册中心注册自己就会报错,因为没有说明注册中心地址,不知道注册到哪里 service-url: defaultzone: http://127.0.0.1:8761/eureka/ #通过引用上面的定义来声明注册地址,有一定局限性主要在hostname这块,上面写死的方式比较灵活,如果一定要用下面这种方法,必要时hostname的映射一定要匹配地址,需要在host文件中做映射 # defaultzone: http://${eureka.instance.hostname}:{server.port}/eureka/
cloudserverapplication.java
package com.tp.soft.cloudserver; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; @springbootapplication @enableeurekaserver public class cloudserverapplication { public static void main(string[] args) { springapplication.run(cloudserverapplication.class, args); } }
启动springboot
至此注册中心搭建完成,目前看到的是0个应用
2、客户端代码:
同样创建一个maven项目
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.tp.soft</groupid> <artifactid>cloud-client</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>cloud-client</name> <description>demo project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <spring-cloud.version>finchley.sr1</spring-cloud.version> </properties> <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.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: client server: port: 8762 eureka: client: eureka-server-port: 8762 register-with-eureka: true service-url: defaultzone: http://127.0.0.1:8761/eureka/
cloudclientapplication.java
package com.tp.soft.cloudclient; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.enableeurekaclient; @springbootapplication @enableeurekaclient public class cloudclientapplication { public static void main(string[] args) { springapplication.run(cloudclientapplication.class, args); } }
创建应用接口 也就是后期所有业务接口源头 注册到注册中心供其他业务互相调用
hellocontroller.java
package com.tp.soft.cloudclient.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class hellocontroller { @getmapping("hello") public string hello(){ return "hello"; } }
启动之后
可以看到client应用已经注册成功,接下来就可以在消费端去直接访问这个应用 不用再重复相同的业务接口了
3、消费端
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.tp.soft</groupid> <artifactid>cloud-consume</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>eureka-consume</name> <description>forward project for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.5.release</version> <relativepath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <spring-cloud.version>finchley.sr1</spring-cloud.version> </properties> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-ribbon</artifactid> <version>1.4.2.release</version> </dependency> <!--断路器--> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-hystrix</artifactid> <version>1.4.4.release</version> </dependency> </dependencies> <dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
application.yml
#本项目端口 server: port: 8763 eureka: client: serviceurl: defaultzone: http://127.0.0.1:8761/eureka/ spring: application: name: service-ribbon
cloudconsumeapplication.java
package com.tp.soft.cloudconsume; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.cloud.client.loadbalancer.loadbalanced; import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate; @springbootapplication @enablediscoveryclient public class cloudconsumeapplication { public static void main(string[] args) { springapplication.run(cloudconsumeapplication.class, args); } @bean @loadbalanced resttemplate resttemplate(){ return new resttemplate(); } }
hellocontroller.java
package com.tp.soft.cloudconsume.controller; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.client.resttemplate; @restcontroller public class hellocontroller { @autowired resttemplate resttemplate; @getmapping("hi") public string hi(){ return resttemplate.getforobject("http://client/hello", string.class); } }
http://对应的应用名/接口名
至此完成配置,有什么不足的,请大牛们不令赐教
上一篇: 老板说话要算话啊
下一篇: 进程同步经典示例 多线程上篇(五)