spring cloud微服务环境搭建

我接触微服务是dubbo开始的,最近公司的使用spring cloud。对于业务系统最主要使用的是一个服务中心eureka,而它与dubbo,nacos思想都是一样的。提供一个注册中心,服务提供者把自己注册到注册中心,调用方从注册中心取到服务者(provider)的信息,再调用provider的接口。

借用nacos的一个图来说明这个过程:

同理eureka也是这三部分,而且每一部分都可部署多个应用。

项目如下图:


代码简单基本上都是三个文件pom.xml,application.properties,Application.java

一、spring-cloud-eureka-server

pom.xml

<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>
	
	 <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.1.0.RELEASE</version>  
        <relativePath/>  
  
    </parent>  
	<groupId>net.highersoft</groupId>
	<artifactId>springcloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud</name>
	<description>eureka服务端</description>  
  
    
  
    <properties>  
  
        <!-- 文件拷贝时的编码 -->  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <!-- 编译时的编码 -->  
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>  
  
        <java.version>1.8</java.version>  
        <maven.compiler.source>1.8</maven.compiler.source>  
        <maven.compiler.target>1.8</maven.compiler.target>  
  
    </properties>  
  
   
  
    <dependencies>  
  
        <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
			<version>1.4.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>
    </dependencies>  
  
    <dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.RELEASE</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.properties

  
  
  
# eureka的默认端口是8761  
server.port=8761


spring.application.name=01.eureka.server
  
####下面2个一定要false,因为这程序是要作为服务端,但是jar中存在eureka-client.jar,所以要false,否则启动会报错的  
#是否注册到eureka
#eureka.client.register-with-eureka=true  
eureka.client.register-with-eureka=false
#是否获取注册信息  
eureka.client.fetch-registry=false
  
#为了便于测试,取消eureka的保护模式,如果启动的话,比如api提供者关闭了,但是eureka仍然保留信息  
eureka.server.enable-self-preservation=false
#服务名称  
eureka.instance.hostname=01.eureka.server
#eureka的服务地址,/eureka是固定的  
#eureka.client.serviceUrl.defaultZone=http://02.server.eureka:8082/eureka/,http://03.server.eureka:8083/eureka/
#eureka.client.serviceUrl.defaultZone=http://02.eureka.server:8082/eureka/,http://03.eureka.server:8083/eureka/
Application.java

package net.highersoft.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer    
@SpringBootApplication  
public class Application {  
  
    public static void main(String[] args) {  
  
        SpringApplication.run(Application.class, args);  
    }  
}  

二、spring-cloud-eureka-provider


<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>net.highersoft</groupId>
  <artifactId>spring-cloud-eureka-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
      <properties>  
  
        <!-- 文件拷贝时的编码 -->  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <!-- 编译时的编码 -->  
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>  
  
        <java.version>1.8</java.version>  
        <maven.compiler.source>1.8</maven.compiler.source>  
        <maven.compiler.target>1.8</maven.compiler.target>  
  
    </properties>  
  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.2.RELEASE</version>  
        <relativePath/>  
  
    </parent>  
  
    <dependencies>  
  
        <!-- 客户端负载均衡 -->  
        <dependency>  
            <groupId>org.springframework.cloud</groupId>  
            <artifactId>spring-cloud-starter-ribbon</artifactId>  
        </dependency>  
  
        <!-- eureka客户端 -->  
        <dependency>  
            <groupId>org.springframework.cloud</groupId>  
            <artifactId>spring-cloud-starter-eureka</artifactId>  
        </dependency>  
  
        <!-- spring boot实现Java Web服务 -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
  
    </dependencies>  
  
    <dependencyManagement>  
        <dependencies>  
            <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-dependencies</artifactId>  
                <version>Dalston.RELEASE</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.properties

  
# eureka的默认端口是8761  
server.port=9081  

###########  
  
spring.application.name=api-user-server
eureka.instance.hostname=127.0.0.1
spring.cloud.config.server.default-application-name=config-server
#eureka的服务地址  
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:8761/eureka/
Application.java

package net.highersoft.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
//https://blog.csdn.net/weixin_36397925/article/details/79496657
//@EnableEurekaClient   
@EnableDiscoveryClient
@SpringBootApplication  
public class Application {  
  
    public static void main(String[] args) {  
  
        SpringApplication.run(Application.class, args);  
    }  
}  
UserProvider.java

package net.highersoft.springcloud.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

  @GetMapping(value="/find/{id}")  
  public String find(@PathVariable("id") String id,HttpServletRequest request){  
      //实际项目中,这里可以使用JSONObject,返回json字符串  
      //为了便于测试消费者app的负载均衡,返回服务端端口  
      String s = "张三2"+"     服务端端口:"+request.getLocalPort();  
        
      return s;  
  }  
}  
三、spring-cloud-eureka-consumer-ribbon

pom.xml

<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>net.highersoft</groupId>
  <artifactId>spring-cloud-eureka-consumer-ribbon</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
      <properties>  
  
        <!-- 文件拷贝时的编码 -->  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
        <!-- 编译时的编码 -->  
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>  
  
        <java.version>1.8</java.version>  
        <maven.compiler.source>1.8</maven.compiler.source>  
        <maven.compiler.target>1.8</maven.compiler.target>  
  
    </properties>  
  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.2.RELEASE</version>  
        <relativePath/>  
  
    </parent>  
  
    <dependencies>  
  
        <dependency>  
            <groupId>org.springframework.cloud</groupId>  
            <artifactId>spring-cloud-starter-eureka</artifactId>  
        </dependency>  
  
    </dependencies>  
  
    <dependencyManagement>  
        <dependencies>  
            <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-dependencies</artifactId>  
                <version>Dalston.RELEASE</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.properties

# eureka的默认端口是8761  
server.port=7081  
server.session-timeout=60  
###########  
  
spring.application.name=app-user
eureka.instance.hostname=127.0.0.1
  
#像eureka服务注册信息时,使用ip地址,默认使用hostname  
eureka.instance.preferIpAddress=true  
#服务的instance-id默认默认值是${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}} ,  
#也就是机器主机名:应用名称:应用端口  
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
#eureka的服务地址  
eureka.client.serviceUrl.defaultZone=http://01.eureka.server:8761/eureka/
eureka.server.enable-self-preservation=false
eureka.instance.prefer-ip-address=true 
Application.java

package net.highersoft.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
//https://blog.csdn.net/weixin_36397925/article/details/79496657
@EnableEurekaClient   
@RibbonClient(name="app-user")  //不可以放在 @SpringBootApplication扫描的包里
@SpringBootApplication  
public class Application {  

    @Bean //定义REST客户端,RestTemplate实例  
    @LoadBalanced //开启负债均衡的能力  
    RestTemplate restTemplate() {  
        return new RestTemplate();  
    }
    
      
    public static void main(String[] args) {  
    	
        SpringApplication.run(Application.class, args);  
    }  
}  
UserController.java

package net.highersoft.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller  
@RequestMapping("/user")  
public class UserController {  
  
    @Autowired  
    private RestTemplate restTemplate;  
    @Autowired  
    private LoadBalancerClient loadBalancerClient;
  
    @GetMapping(value = "/find")  
    @ResponseBody  
    public String find() {  
        //url中对应api提供者的名称,全大写
        String s = restTemplate.getForEntity("http://API-USER-SERVER:9081/user/find/123", String.class).getBody();  
        return s;
    }  
  
}  

最后在浏览器里输入:http://localhost:7081/user/find

可以看到结果吧,这个就是请求Consumer从Provider返回的结果。

源码:
链接: https://pan.baidu.com/s/1UXKOM5ffivEbO-iWd5f0_w 提取码: vnte

下一文再贴下nacos的代码。

nacos环境搭建

文/程忠 浏览次数:0次   2019-08-30 22:40:39

相关阅读

微信扫描-捐赠支持
加入QQ群-技术交流

评论:
点击刷新

↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑